package com.LeetCode.String;

import org.junit.Test;

import java.util.Arrays;

/**
 * 无重复字符的最长子串
 */
public class LongestNoDupString {

    public int lengthOfLongestSubstring(String s) {
        //记录该字符上一次出现位置
        int[] last = new int[128];
        Arrays.fill(last,-1);
        int len = s.length();
        int res = 0;
        int start = 0;
        for(int i = 0; i < len; i++){
            int index = s.charAt(i);
            start = Math.max(start , last[index] + 1);
            res = Math.max(res , i - start + 1);
            last[index] = i;
        }
        return res;
    }

    @Test
    public void test(){
        String str = "pwwkew";
        System.out.println(lengthOfLongestSubstring(str));
    }

}
